package com.ggkt.common.utils;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Map;

public class HttpClientUtil {
    /**
     * 封装HTTP get方法
     */
    public static InputStream get(String url)  {

        GetMethod getMethod = new GetMethod(url);
        HttpClient httpClient = new HttpClient();
        //设置连接超时时间(单位毫秒)
        httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(15000);
        //设置读数据超时时间(单位毫秒)
        httpClient.getHttpConnectionManager().getParams().setSoTimeout(30000);
        httpClient.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "utf-8");
        //发送状态
        int statusCode;
        try {
            statusCode = httpClient.executeMethod(getMethod);
            //System.out.println("statusCode:"+statusCode);
            InputStream inputStream=null;
            if (statusCode == 200) {
                inputStream = getMethod.getResponseBodyAsStream();
            }
            return inputStream;
        } catch (HttpException e) {
            e.printStackTrace();
            return null;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }

    }
    public static String sendPostMethod(String url,String timeStamp,String signature,byte[] bodyBytes) throws HttpException, IOException {
        PostMethod postMethod = new PostMethod(url);
        postMethod.setRequestHeader("Content-type", "application/json; charset=utf-8");
        postMethod.setRequestHeader("Timestamp",timeStamp);
        postMethod.setRequestHeader("Signature",signature);
        postMethod.setRequestBody(new String(bodyBytes));

        HttpClient httpClient = new HttpClient();
        httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(15000);//设置连接超时时间(单位毫秒)
        httpClient.getHttpConnectionManager().getParams().setSoTimeout(30000);//设置读数据超时时间(单位毫秒)
        int statusCode = httpClient.executeMethod(postMethod);
        String returnStr = postMethod.getResponseBodyAsString();
        return returnStr;

    }

    public static String sendGetMethod(String url) throws HttpException, IOException {
        GetMethod getMethod = new GetMethod(url);
        getMethod.setRequestHeader("Content-type", "application/json; charset=utf-8");
        HttpClient httpClient = new HttpClient();
        httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(15000);//设置连接超时时间(单位毫秒)
        httpClient.getHttpConnectionManager().getParams().setSoTimeout(30000);//设置读数据超时时间(单位毫秒)
        int statusCode = httpClient.executeMethod(getMethod);
        String returnStr = getMethod.getResponseBodyAsString();
        return returnStr;

    }

    @SuppressWarnings("rawtypes")
    public static StringBuffer getStringBufferPostNew(Map<String,Object> paraMap,String url) throws Exception{
        StringBuffer sb = new StringBuffer();
        PostMethod postMethod = new PostMethod(url);
        for(Map.Entry entry:paraMap.entrySet()){
            String key = entry.getKey().toString();
            String value = entry.getValue().toString();
            postMethod.setParameter(key,value);
        }

        HttpClient httpClient = new HttpClient();
        //设置连接超时时间(单位毫秒)
        httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(15000);
        //设置读数据超时时间(单位毫秒)
        httpClient.getHttpConnectionManager().getParams().setSoTimeout(30000);
        httpClient.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "utf-8");
        //发送状态
        try {
            int statusCode = httpClient.executeMethod(postMethod);
            if (statusCode == 200) {
                InputStream inputStream = postMethod.getResponseBodyAsStream();
                BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
                StringBuffer stringBuffer = new StringBuffer();
                String str= "";
                while((str = br.readLine()) != null){
                    stringBuffer.append(str);
                }
                sb = stringBuffer;
            }else{
                sb.append("{\"message\": \"请求接口不通statusCode"+statusCode+"\", \"code\":1}");
            }
        } catch (Exception e) {
            sb.append("{\"message\": \"请求接口异常，"+e.toString()+"\", \"code\":1}");
        }
        return sb;
    }

    public static String postJsonBody(String url, String json) {
        PostMethod postMethod = new UTF8PostMethod(url);
        postMethod.setRequestHeader("Content-type", "application/json; charset=utf-8");
        postMethod.setRequestBody(json);

        HttpClient httpClient = new HttpClient();
        httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(3000);//设置连接超时时间(单位毫秒)
        httpClient.getHttpConnectionManager().getParams().setSoTimeout(3000);//设置读数据超时时间(单位毫秒)
        int statusCode = 0;
        String result = null;
        try {
            statusCode = httpClient.executeMethod(postMethod);
            result = postMethod.getResponseBodyAsString();
        } catch (IOException e) {
            e.printStackTrace();
        }

        System.out.println("statusCode: " + statusCode + ", result：" + result);

        return result;
    }

    //创建PostMethod的子类设置编码格式
    private static class UTF8PostMethod extends PostMethod{
        public UTF8PostMethod(String url){
            super(url);
        }
        @Override
        public String getRequestCharSet() {
            return "UTF-8";
        }
    }

    public static void main(String[] args) {
        HttpClientUtil.postJsonBody("http://m.ggkt.com/nqun/allocation/parseJinQunBaoMembersInfo", "");
    }

}
